package com.blockmeta.bbs.minemodule.adapter;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;


import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.businesslibrary.adapter.expandablelist.BaseViewHolder;
import com.blockmeta.bbs.businesslibrary.adapter.expandablelist.GroupedRecyclerViewAdapter;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.business.UmengEvent;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.util.DeepLinkUtils;
import com.blockmeta.bbs.minemodule.R;
import com.blockmeta.bbs.minemodule.pojo.CoinCenterPOJO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author AirThor
 * @date 2018/10/16.
 * 任务中心列表适配器
 */
public class CoinTaskCenterAdapter extends GroupedRecyclerViewAdapter {
    private ArrayList<CoinCenterPOJO.TasksBean> mGroups;
    private Context mContext;

    public CoinTaskCenterAdapter(Context context, ArrayList<CoinCenterPOJO.TasksBean> groups) {
        super(context);
        mGroups = groups;
        mContext = context;
    }

    @Override
    public int getGroupCount() {
        return mGroups == null ? 0 : mGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<CoinCenterPOJO.TasksBean.ListBean> children = mGroups.get(groupPosition).list;
        return children == null ? 0 : children.size();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.item_group_title;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return 0;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_task_center;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
        CoinCenterPOJO.TasksBean tasksBean = mGroups.get(groupPosition);
        holder.setText(R.id.tv_group_title,tasksBean.type);
    }

    @Override

    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {

    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {
        final CoinCenterPOJO.TasksBean.ListBean listBean = mGroups.get(groupPosition).list.get(childPosition);
        final String taskType = mGroups.get(groupPosition).type;
        final String taskDetail = listBean.content;
        holder.setText(R.id.tv_task_name,listBean.content);
        String coins = listBean.award+"金币";
        holder.setText(R.id.tv_coin_count,coins);
        Button button = holder.get(R.id.btn_go);
        if (listBean.done){
            button.setEnabled(false);
            button.setText("已领取");
        }else {
            button.setEnabled(true);
            button.setText("去完成");
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("blockmeta://praiseApp".equals(listBean.link)){
                    Uri market = Uri.parse("market://details?id=" + mContext.getPackageName());
                    Intent intentMarket = new Intent(Intent.ACTION_VIEW, market);
                    intentMarket.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    ComponentName componentName = intentMarket.resolveActivity(mContext.getPackageManager());
                    if (componentName == null) {
                        ToastUtil.show("您尚未安装应用市场");
                    } else {
                        if (mContext instanceof BaseActivity) {
                            ((BaseActivity) mContext).isTaskCompleteNoHint(12);
                        }
                        mContext.startActivity(intentMarket);
                    }
                }else {
                    DeepLinkUtils.jumpToTarget(mContext, Uri.parse(listBean.link));
                }
                HashMap<String, String> map = new HashMap<>(4);
                map.put("task_type_ID",taskType);
                map.put("task_detail_ID",taskDetail);
                UmengEvent.eventStatistics(mContext, Constant.U_MINE_task_go,map);
//                if (mContext instanceof Activity){
//                    ((Activity) mContext).finish();
//                }
            }
        });
    }
}
